package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/courseSelection/model"
	"strconv"
)

type CourseSelectionController struct {
	WhatWeek *model.WhatWeek
	CourseArrangement *model.CourseArrangement
}

func (csc *CourseSelectionController)SelectClass(c *fiber.Ctx){

	csc.WhatWeek = new(model.WhatWeek)

	studentIdStr := c.FormValue("studentId")
	studentId, err := strconv.ParseInt(studentIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	courseArrangementStr := c.FormValue("courseArrangement")
	var courseArrangement model.CourseArrangement
	uErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(courseArrangementStr), &courseArrangement)
	if uErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := csc.WhatWeek.SelectClass(studentId, &courseArrangement)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (csc *CourseSelectionController)CreateCourse(c *fiber.Ctx){

	csc.CourseArrangement = new(model.CourseArrangement)

	var courseArrangement model.CourseArrangement
	c.BodyParser(&courseArrangement)

	res , err := csc.CourseArrangement.CreateCourse(&courseArrangement)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (csc *CourseSelectionController)QueryClassCard(c *fiber.Ctx){

	csc.WhatWeek = new(model.WhatWeek)

	studentIdStr := c.FormValue("studentId")
	studentId, err := strconv.ParseInt(studentIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := csc.WhatWeek.QueryClassCard(studentId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}
